<?php
/**
* object-oriented file system manipulation as nodes.
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package pathnodes
* @subpackage phplib
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/**
* registers as pathnode descendant
*/
pathnode::heritage('phplibfolder',20);
/**
* php libraries folder node
*/
class phpLibFolder extends folderNode {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
    /**
    *
    * @access
    */
    function phpLibFolder($path) {
        $this->__construct($path);
    }/**/

    /**
    * real constructor
    * @access
    */
    function __construct($path) {
        parent::__construct($path);
        //$this->_loadMeta();
        $this->map=&new yamlFileNode($this->absolute_path.'/lib-index.yml');
        $this->map->load();
        // autosaving index
        register_shutdown_function(array($this, '__destruct'));
    }/**/

    /**
    *
    */
    function __destruct() {
        $this->map->save();
    }
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
    /**
    *
    * @param
    * @access public
    * @return
    */
    function selfDetector($path) {
        return is_dir($path) and $path=='lib';
    }/**/

    /**
    * search for class definition file
    * @param string $class class name
    * @access public
    * @return false|string file full path
    */
    function locateClassDef($class) {
        // searching into map index
        if ($file=$this->map->getData('classes.'.$class.'.file'))
            return $this->getRelativePath().'/'.$file;
        // searching by name convention
        if ($file=$this->_searchClassFileByNameConvention($class)) {
            return $this->getRelativePath().'/'.$file;
        }
    }/**/

    /**
    * return class required classes
    * @param string $class
    * @access public
    * @return array
    */
    function getClassRequiredClasses($class) {
        return $this->map->getData('classes.'.$class.'.required_classes');
    }/**/

    /**
    * include class definition file
    * @param string $class
    * @access public
    * @return bool
    */
    function defineClass($class) {
        if (!$file=$this->locateClassDef($class))
            return false;
        //echo "<p>including $file</p>";
        //$fnode=&new filenode($file);
        //$file->read();
        if (!include($file)) {
            trigger_error('phplibfolder->defineClass could not find file '.$file);
            // dirty hack for some web hosts using absolute paths for includes
            include('/'.$file);
        }

        return class_exists($class);
    }/**/

    /**
    *
    * @param
    * @access public
    * @return
    * /
    function () {

    }/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
    /**
    * @var yamlFileNode library index map
    */
    var $map; // metadata inifilenode instance
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
    /**
    * searches class definition file into foldernode by name convention
    * convention is : {class}[.{parent-class}].class.php
    * recursive search
    * @access private
    * @param string $class
    * @return string file path
    */
    function _searchClassFileByNameConvention($class) {
        $regex='^'.$class.'(\.([a-zA-Z0-9_]+))?\.class\.php(4|5)?$';
        if (!$results=$this->regexSearchName($regex,true))
            return false;
        $fileNode=$results[0];
        if (!is_a($fileNode,'filenode')) {
            echo wakapi::vardump($results);
            return false;
        }
        // registering heritage dependancy
        // echo "<p>$regex : ".$fileNode->getBasename()."</p>";
        //$regex2='^'.$class.'\.([a-zA-Z0-9_]+)\.class\.php(4|5)?$';
        /*if ($search=$fileNode->regexSelfName($regex2)) {
            // registering dependancy
            //$this->map
            $parent=$search[1];
            //echo $this->deb->vardump($search);
            //$this->_registerFileClassDependancy($fileNode->getRelativePath(),$search[1]);
        }*/
        $file=$fileNode->getRelativePath();
        // registering class
        $this->_registerClass($class,$file);
        return $file;
    }/**/

    /**
    * registers a class, with dependancies etc
    * @param
    * @access protected
    * @return
    */
    function _registerClass($class,$file) {
        // getting current data
        //$data=$this->map->getData('classes.'.$class);
        $data['file']=$file;

        $node=&new phplibfile($file);

        $data['required_classes']=$node->requiredClasses();
        $this->map->setData($data,'classes.'.$class);
        return true;
    }/**/

    /**
    *
    * @param
    * @access protected
    * @return
    * /
    function () {

    }/**/
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
    /**
    * loads meta data from ini file
    * @param
    * @access private
    * @return bool
    * /
    function _loadMeta($file=null) {
        if (!$file) $file=$this->path.'/phplibfolder.meta.ini';
        if (!$this->meta=pathnode::getnode($file))
            return false;
        $deb=wak::getservice('wakdebug');
        $this->meta->load();
        //  echo $deb->vardump($this->meta);
        return $this->meta->load();
    }/**/

    /**
    *
    * @param
    * @access private
    * @return
    * /
    function () {

    }/**/
}
?>
